//
//  HMHomeTableViewController.swift
//  WeiBo
//
//  Created by Feng on 2017/6/4.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit

class HMHomeTableViewController: HMVisitorTableViewController {

    //MARK: --懒加载控件
    //用户登录信息及其微博数组
    lazy var statusViewModel: HMStatusViewModel = HMStatusViewModel()
    //上拉加载视图(菊花)
    lazy var pullUpView: UIActivityIndicatorView = {
       
        let pullView = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.whiteLarge)
        
        pullView.color = UIColor.orange
        
        return pullView
    }()
    //下拉刷新视图 refreshControl
    lazy var pullDownView: HMRefreshControl = {
        
        let refreshControl = HMRefreshControl()
        //注册监听事件
        refreshControl.addTarget(self, action: #selector(pullDownRefresh), for: .valueChanged)
        
        return refreshControl
    }()
    
    //tip Label:提示加载了多少条微博数据
    lazy var tipLabel: UILabel = {
        let label = UILabel(fontSize: 14, textColor: UIColor.white)
        
        label.backgroundColor = UIColor.orange
        label.textAlignment = .center
        label.text = "没有加载到微博数据"
        //默认隐藏
        label.isHidden = true
        
        return label
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !isLogin
        {
            visitorView?.updateInfoView(imageName: nil, labelText: nil)
        }
        else
        {
            setUI()
            
            self.loadData()
        }
    }
    
    func setUI()
    {
        if let nav = self.navigationController
        {
            nav.view.insertSubview(tipLabel, belowSubview: nav.navigationBar)
            //设置frame
            tipLabel.frame = CGRect(x: 0, y: nav.navigationBar.frame.maxY - 35, width: nav.navigationBar.width, height: 35)
        }
        
        self.setupTableView()
    }
    
    func loadData()
    {
        //登录成功
        statusViewModel.loadData(isUp: pullUpView.isAnimating, callBack: { (isSuccess,message) in
            if !isSuccess
            {
                print("首页加载出错")
                return
            }
            else
            {
                //只要不是上拉加载,就执行tip动画
                if !self.pullUpView.isAnimating
                {
                    self.startTipAnimation(message: message)
                }
                self.tableView.reloadData()
            }
            
            //上拉加载数据成功后,结束动画(菊花)
            self.pullUpView.stopAnimating()
            //下拉加载数据后,结束动画
            self.pullDownView.endRefreshing()
        })
    }
    
    func setupTableView()
    {
        //注册
        tableView.register(HMHomeTableViewCell.self, forCellReuseIdentifier: HomeCellIdentifier)
        //设置分割线
        tableView.separatorStyle = .none
        //设置自动计算行高
        tableView.rowHeight = UITableViewAutomaticDimension
        //设置预估行高
        tableView.estimatedRowHeight = 200
        //设置加载视图为footerView
        tableView.tableFooterView = pullUpView
        
        //添加pullDownView
        tableView.addSubview(pullDownView)
        
    }
    
    //refreshControl监听事件
    func pullDownRefresh()
    {
        loadData()
    }
    
    //tip动画
    private func startTipAnimation(message: String)
    {
        //tip没隐藏,说明还在执行动画,就不能再次执行动画
        if tipLabel.isHidden == false
        {
            return
        }
        
        tipLabel.text = message
        tipLabel.isHidden = false
        
        UIView.animate(withDuration: 1, animations: { 
            //更改transform
            self.tipLabel.transform = CGAffineTransform(translationX: 0, y: self.tipLabel.height)
            
        }) { (_) in
            UIView.animate(withDuration: 1, animations: { 
                self.tipLabel.transform = CGAffineTransform.identity
            }, completion: { (_) in
                self.tipLabel.isHidden = true
            })
        }
    }
}

extension HMHomeTableViewController
{
    override func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusViewModel.statusViewModelArray.count
//        return statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: HomeCellIdentifier, for: indexPath) as! HMHomeTableViewCell
        
        let model = statusViewModel.statusViewModelArray[indexPath.row]
        cell.statusCellViewModel = model
        
        cell.selectionStyle = .none
        
        return cell
    }
    
    //cell将要显示的时候
    override func tableView(_ tableView: UITableView, willDisplay cell: UITableViewCell, forRowAt indexPath: IndexPath) {
        //当上拉到底部,pullUpView(菊花)开启动画
        if indexPath.row == statusViewModel.statusViewModelArray.count - 1 && !pullUpView.isAnimating
        {
            pullUpView.startAnimating()
            loadData()
        }
    }
}
